package com.study.testSpringBoot.util;

import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;
import reactor.netty.http.client.HttpClient;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

/**
 * @author wc_phz
 * @ClassName TestWebFlux
 * @Description: [ ]
 * @date 2020/3/5 14:43
 */
public class TestWebFlux {

    public static void main(String[] args) {
//        Mono<String> mono = WebClient.create().post()
//                .uri("http://device-haier-default.cosmoiiot.haier.net/"
//                + "model/opcua/queryDetailByRootUri?uri=/Project_adapt_haier/"
//                + "5e21729dff6bce0008f5cf8d&extendParentProperties=true")
//                .contentType(MediaType.APPLICATION_JSON)
//
//                .retrieve().bodyToMono(String.class);
//        String s = mono.block();
//        System.out.println("aaa");
        Map<String, Object> map = new HashMap<>();
        map.put("uri","/Project_adapt_haier/5e21729dff6bce0008f5cf8d");
        map.put("extendParentProperties",true);
        String s = sendPost("http://device-haier-default.cosmoiiot.haier.net/model/opcua/queryDetailByRootUri", map);
        System.out.println(s);
    }

    /**
     * 发送http post的请求
     * @param realurl
     * @param map
     * @return
     */
    public static String sendPost(String realurl, Map<String,Object> map){
        try {
            //访问准备
            URL url = new URL(realurl);
            //开始访问
            StringBuilder postData = new StringBuilder();
            for (Map.Entry<String,Object> param : map.entrySet()) {
                if (postData.length() != 0) {postData.append('&');}
                postData.append(URLEncoder.encode(param.getKey(), "UTF-8"));
                postData.append('=');
                postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));
            }
            byte[] postDataBytes = postData.toString().getBytes("UTF-8");

            HttpURLConnection conn = (HttpURLConnection)url.openConnection();
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            conn.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
            conn.setDoOutput(true);
            conn.getOutputStream().write(postDataBytes);
            conn.setConnectTimeout(30000);
            conn.setReadTimeout(30000);

            Reader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));

            StringBuilder sb = new StringBuilder();
            for (int c; (c = in.read()) >= 0;) {
                sb.append((char) c);
            }
            String response = sb.toString();
//			System.out.println(response);
            in.close();
            conn.disconnect();
            return response;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
